/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil.IGetServicesDataCallBack;
import aym.util.json.JsonMap;
import aym.util.json.JsonMapOrListJsonMap2JsonUtil;
import aym.util.json.JsonParseHelper;
import com.google.gson.Gson;
import net.tsz.afinal.annotation.view.ViewInject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.ShopAddOrderProductAdapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.*;
import so.shanku.essential.view.ListViewNoScroll;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tanghuan
 * @ClassName: ShoppingAddOrderActivity
 * @Description: TODO(填写订单类)
 * @date 2014年12月3日 下午7:51:44
 */
public class ShoppingAddOrderActivity extends BaseUIActivity {
	/**
	 * 收货人布局
	 */
	@ViewInject(id = R.id.s_a_o_ll_shouhuorenxinxi, click = "selectShouHuoRan")
	private LinearLayout ll_shouhuoren;
	/**
	 * 收货人
	 */
	@ViewInject(id = R.id.s_a_o_tv_shouhuoren)
	private TextView tv_shouhuren;
	/**
	 * 收货人电话
	 */
	@ViewInject(id = R.id.s_a_o_tv_phone)
	private TextView tv_shouhuren_dianhua;
	/**
	 * 收货人地址
	 */
	@ViewInject(id = R.id.s_a_o_tv_address)
	private TextView tv_shouhuren_dizhi;
	/**
	 * 支付和配送的点击选择
	 */
	@ViewInject(id = R.id.s_a_o_ll_payandsend, click = "payandsend")
	private LinearLayout ll_payandsend;
	/**
	 * 支付信息方式
	 */
	@ViewInject(id = R.id.s_a_o_tv_payway)
	private TextView tv_zhifu;

	/**
	 * 配送方式
	 */
	@ViewInject(id = R.id.s_a_o_tv_sendway)
	private TextView tv_peisong;
	/**
	 * 配送时间
	 */
	@ViewInject(id = R.id.s_a_o_tv_sendtime)
	private TextView tv_peisong_time;
	/**
	 * 配送是否电话通知
	 */
	@ViewInject(id = R.id.s_a_o_tv_iscall)
	private TextView tv_peisong_checkphone;

	/**
	 * 普通发票展示信息
	 */
	@ViewInject(id = R.id.s_a_o_tv_fapiaoxinxi)
	private TextView tv_fapiao_pugong;
	/**
	 * 发票类型信息
	 */
	@ViewInject(id = R.id.s_a_o_rg_fapiaoleixing)
	private RadioGroup rg_fapiao_leixing;
	/**
	 * 发票抬头信息
	 */
	@ViewInject(id = R.id.s_a_o_et_fapiaotaitou)
	private EditText et_fapiao_taitou;
	/**
	 * 备注
	 */
	@ViewInject(id = R.id.s_a_o_et_note)
	private EditText et_note;
	/**
	 * 优惠券的点击选择
	 */
	@ViewInject(id = R.id.s_a_o_ll_youhuiquan, click = "coupon_click")
	private LinearLayout ll_coupon;
	/**
	 * 普通发票展示信息
	 */
	@ViewInject(id = R.id.s_a_o_tv_youhuiquan)
	private TextView tv_coupon;
	/**
	 * 返回购物车修改
	 */
	@ViewInject(id = R.id.s_a_o_tv_backshoppingcart, click = "goShoppingCart")
	private TextView tv_goshoppingcart;
	/**
	 * 返回购物车修改
	 */
	@ViewInject(id = R.id.s_a_o_lvns_prolist)
	private ListViewNoScroll lvns_proList;
	/**
	 * 商品金额
	 */
	@ViewInject(id = R.id.s_a_o_tv_shangpinprice)
	private TextView tv_qian_shangpin;
	/**
	 * 运费
	 */
	@ViewInject(id = R.id.s_a_o_tv_yunfei)
	private TextView tv_qian_yunfei;
	/**
	 * 活动优惠
	 */
	@ViewInject(id = R.id.s_a_o_tv_fanxian)
	private TextView tv_qian_fanxian;

	int flog = 0;
	/**
	 * 优惠券
	 */
	@ViewInject(id = R.id.s_a_o_tv_privilege_discount)
	private TextView tv_privilege_discount;
	/**
	 * 应付总额
	 */
	@ViewInject(id = R.id.s_a_o_tv_tatalpay)
	private TextView tv_qian_yingfuzonge;
	/**
	 * 应付金额
	 */
	@ViewInject(id = R.id.s_a_o_tv_totalmoney)
	private TextView tv_qian_yingfujine;

	/**
	 * 提交订单
	 */
	@ViewInject(id = R.id.s_a_o_ib_commitorder, click = "commit_order_click")
	private Button ib_tijiao_dingdan;

	/**
	 * 选择积分
	 */
	@ViewInject(id = R.id.select_jifen_bt, click = "select_jifen_bt_click")
	private Button select_jifen_bt;

	/**
	 * 填写积分
	 */
	@ViewInject(id = R.id.jifen_ed)
	private EditText jifen_ed;
	//可用积分
	@ViewInject(id = R.id.jifen_availble_tv)
	private TextView jifen_availble_tv;

	//可用优惠
	@ViewInject(id = R.id.s_a_o_tv_jifen_duihuan)
	private TextView jifen_youhui_tv;


	//应付金额
	private double ActualPayPrice;
	/**
	 * 由购物车传入的订单商品信息 json格式
	 */
	private String selectProJson;
	/**
	 * 由购物车传入的订单商品信息
	 */
	private List<JsonMap<String, Object>> selectProduct;
	/**
	 * 由购物车传入的订单商品信息2
	 */
	private List<JsonMap<String, Object>> selectProduct2;
	/**
	 * 本次订单的收货信息的id 支付信息的id
	 */
	private String addressId="0", payId;
	// 保留两位小数
	DecimalFormat fmt = new DecimalFormat("0.00");
	/**
	 * 使用优惠券的id
	 */
	String couponid = "0";

	//可用积分
	private float jifen_num_available = 0;
	private float jifen_num_totle = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shopping_add_order);
		setCenter_title(R.string.shopping_add_order_title);
		Intent intent = getIntent();
		double zongjia = intent.getDoubleExtra(ExtraKeys.Key_Msg1, 0.00);
		selectProJson = intent.getStringExtra(ExtraKeys.Key_Msg2);
		selectProduct = JsonParseHelper.getList_JsonMap(selectProJson);
		selectProduct2 = new ArrayList<JsonMap<String, Object>>();
		// for (JsonMap<String, Object> jsonMap : selectProduct) {
		// Log.d(TAG, jsonMap.toJson());
		// }
		setAdapter_porList();
		tv_qian_shangpin.setText(StringUtil.getFormatMoneyWithSignMust(zongjia));
		tv_qian_fanxian.setText(StringUtil.getFormatMoneyWithSignMust("0"));
		jifen_youhui_tv.setText(StringUtil.getFormatMoneyWithSignMust("0"));
		tv_qian_yunfei.setText(StringUtil.getFormatMoneyWithSignMust("0"));
		tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(zongjia));
		tv_qian_yingfuzonge.setText(StringUtil.getFormatMoneyWithSignMust(zongjia));
		tv_privilege_discount.setText(StringUtil.getFormatMoneyWithSignMust("0"));

		getData_getDefInfo();

		et_fapiao_taitou.setText("");
		registerReceiver(broadcastReceiver,new IntentFilter(Constant.ACT_PAY_OVER));
//		et_fapiao_taitou.addTextChangedListener(editTextContentChangeListener);
//		et_note.addTextChangedListener(editTextContentChangeListener);
//		jifen_ed.addTextChangedListener(editTextContentChangeListener);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(broadcastReceiver);
	}

	BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			mContext.setResult(RESULT_OK);
			mContext.finish();
		}
	};
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == 1) {
				String shouhuoren = data.getStringExtra(ExtraKeys.Key_Msg1);
				String dianhua = data.getStringExtra(ExtraKeys.Key_Msg2);
				String dizhi = data.getStringExtra(ExtraKeys.Key_Msg3);
				addressId = data.getStringExtra(ExtraKeys.Key_Msg4);
				changeReceiverAddress();
				flushShouHuoRen(shouhuoren, dianhua, dizhi);
			} else if (requestCode == request_payType) {
				// flushpayandsend(JsonParseHelper.getJsonMap(data
				// .getStringExtra(ExtraKeys.Key_Msg1)));
				String zhifu = data.getStringExtra(ExtraKeys.Key_Msg1);
				payId = data.getStringExtra(ExtraKeys.Key_Msg2);

				flushpayandsend(zhifu);
			} else if (requestCode == 3) {
				String Price = data.getStringExtra(ExtraKeys.Key_Msg1);
				if (Price != null) {
					double ActualPayPrice = Double.valueOf(
							data.getStringExtra(ExtraKeys.Key_Msg1))
							.doubleValue();
					double Coupon = Double.valueOf(
							data.getStringExtra(ExtraKeys.Key_Msg2))
							.doubleValue();
					couponid = data.getStringExtra(ExtraKeys.Key_Msg3);
					flushJieSuanqingdan(ActualPayPrice, Coupon);
				}

			}
		}
		if (requestCode == commit_request) {
			setResult(RESULT_OK);
			finish();
		}

	}

	/**
	 * 用户更改地址
	 */
	private void changeReceiverAddress() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key,
				GetDataConfing.Key_shoppingUsedCoupon);
		params.put("userAccount", Utils.getUserAccount(mContext));
		params.put("ShoppingCartList", selectProduct);
		couponid=TextUtils.isEmpty(couponid)?"0":couponid;
		params.put("couponItemId", couponid);
		addressId=TextUtils.isEmpty(addressId)?"0":addressId;
		params.put("addressId", addressId);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_shoppingUsedCoupon);
		queue.setCallBack(callBack);
		queue.setParamsNoJson(params);
		queue.setWhat(what_changeAddress);
		getDataUtil.getData(queue);
	}

	/**
	 * 刷新结算清单  使用优惠券
	 */
	private void flushJieSuanqingdan(double actualPayPrice, double coupon) {
		ActualPayPrice = actualPayPrice;
		tv_privilege_discount.setText(StringUtil.getFormatMoneyWithSignMust("" + coupon));
		tv_qian_yingfuzonge.setText(StringUtil.getFormatMoneyWithSignMust( ActualPayPrice));
		tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		//tv_qian_yunfei.setText(StringUtil.getFormatMoneyWithSignMust("" + freinghtPrice));

		tv_coupon.setText(R.string.cancel_rivilege);
		flog = 1;
	}



	private void flushBottomView(JsonMap jsonMap) {
		ActualPayPrice=jsonMap.getDouble("ActualPayPrice");
		//tv_privilege_discount.setText(StringUtil.getFormatMoneyWithSignMust(jsonMap.getStringNoNull("CouponDiscountMoney")));
		tv_qian_yingfuzonge.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		tv_qian_yunfei.setText(StringUtil.getFormatMoneyWithSignMust(jsonMap.getStringNoNull("FreightPrice")));
		//tv_qian_fanxian.setText(StringUtil.getFormatMoneyWithSignMust(jsonMap.getStringNoNull("OrderEventDiscountMoney")));
	}

	/**
	 * 刷新结算清单  取消优惠券
	 *
	 * @param data
	 */
	private void flushJieSuanqingdan(JsonMap<String, Object> data) {
		ActualPayPrice = Double.valueOf(
				data.getStringNoNull("ActualPayPrice", "0")).doubleValue();
		tv_privilege_discount.setText(StringUtil.getFormatMoneyWithSignMust(0.0));
		tv_qian_yingfuzonge.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		tv_coupon.setText(R.string.discount_coupon);
		flog = 0;
		couponid = "0";
	}


	/**
	 * 刷新获取到的默认信息
	 */
	private void flushDefInfo(JsonMap<String, Object> data) {
		if (data.size() < 4) {
			ShowGetDataError.showNetError(mContext);
		}
		selectProduct2 = data.getList_JsonMap("ShoppingCartList");
		setAdapter_porList();
		JsonMap<String, Object> priceInfo = data
				.getJsonMap("ShoppingCartHeaderInfo");

		// 修改 订单显示


		ActualPayPrice = Double.valueOf(
				priceInfo.getStringNoNull("ActualPayPrice", "0")).doubleValue();


//		try {
//			Float floadt=new Float(data.getStringNoNull("AvailablePoint"));
//			jifen_num_available=floadt.intValue();
//			jifen_num_totle=floadt.intValue();
//		} catch (NumberFormatException e) {
//			e.printStackTrace();
//		}
		jifen_availble_tv.setText("" + jifen_num_available);
		tv_qian_shangpin.setText(StringUtil.getFormatMoneyWithSignMust(priceInfo.getStringNoNull("TotalPrice", "0")));
		tv_qian_fanxian.setText(StringUtil.getFormatMoneyWithSignMust(priceInfo.getStringNoNull("OrderEventDiscountMoney", "0")));
		 tv_qian_yunfei.setText(StringUtil.getFormatMoneyWithSignMust(priceInfo.getStringNoNull("FreightPrice", "0")));
		tv_privilege_discount.setText(StringUtil.getFormatMoneyWithSignMust(0));
		tv_qian_yingfuzonge.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));

		// end
		JsonMap<String, Object> shouhuorenInfo = data
				.getJsonMap("DeliveryAddress");
		flushShouHuoRen(shouhuorenInfo.getStringNoNull("consignee"),
				shouhuorenInfo.getStringNoNull("phone"),
				shouhuorenInfo.getStringNoNull("consigneeAddress"));
		addressId = shouhuorenInfo.getStringNoNull("id");

		JsonMap<String, Object> zhifuInfo = data.getJsonMap("GetpayType");
		flushpayandsend(zhifuInfo.getStringNoNull("Name"));
		payId = zhifuInfo.getStringNoNull("Id");

	}

	/**
	 * 刷新收货人信息
	 */
	private void flushShouHuoRen(String shr, String dh, String dz) {
		tv_shouhuren.setText(shr);
		tv_shouhuren_dianhua.setText(dh);
		tv_shouhuren_dizhi.setText(dz);
	}

	/**
	 * 刷新支付和配送信息
	 */
	private void flushpayandsend(String info) {
		tv_zhifu.setText(info);
	}

	private int commit_request = 100;
	/**
	 * 生成订单后 设置为true
	 */
	private boolean isCommitedSuccess = false;

	/**
	 * 去生成订单页面
	 */
	private void goCommited(JsonMap<String, Object> order) {
		LogUtil.d(TAG,"startPay");
		toast.showToast(R.string.order_create_success);
		JsonMapOrListJsonMap2JsonUtil<String, Object> util = new JsonMapOrListJsonMap2JsonUtil<String, Object>();
		Intent intent = new Intent(this, CommitOrderActivity.class);
		intent.putExtra(ExtraKeys.Key_Msg1, 3);
		intent.putExtra(ExtraKeys.Key_Msg2, util.map2Json(order));
		startActivityForResult(intent, commit_request);
	}

	/**
	 * 为已选定的商品设定数据源
	 */
	private void setAdapter_porList() {
		ShopAddOrderProductAdapter adapter = new ShopAddOrderProductAdapter(this, selectProduct2);
		lvns_proList.setAdapter(adapter);
//		BaseAdapter adapter = new HasClickAdapter(this, selectProduct,
//				R.layout.item_shopping_addrder, new String[] {
//						"ProductSaleName", "Amount" },
//				new int[] { R.id.i_s_a_o_tv_productname,
//						R.id.i_s_a_o_tv_productnum }, 0);
//		lvns_proList.setAdapter(adapter);
	}

	/**
	 * 去选择收货信息
	 */
	public void selectShouHuoRan(View v) {
		Intent intent = new Intent(this, ShoppingUserRecipientActivity.class);
		intent.putExtra(ExtraKeys.Key_Msg1, addressId);
		startActivityForResult(intent, 1);
	}

	int request_payType = 2;
	/**
	 * 去选择支付和配送信息
	 */
	public void payandsend(View v) {
		Intent intent = new Intent(this, ShoppingZhifuPeisongWayActivity.class);
		intent.putExtra(ExtraKeys.Key_Msg1, 1);
		startActivityForResult(intent, request_payType);
	}

	/**
	 * 返回购物车修改
	 */
	public void goShoppingCart(View v) {
		this.finish();
	}

	/**
	 * 提交订单
	 */
	public void commit_order_click(View v) {
		// this.finish();
		if (TextUtils.isEmpty(addressId) || TextUtils.isEmpty(payId)) {
			//toast.showToast(R.string.shopping_add_order_addressorpayisnull);
			toast.showToast(R.string.shopping_add_order_payisnull);
			return;
		}
		getData_tijiao(et_fapiao_taitou.getText().toString());
	}

	/**
	 * 调用服务器提交订单
	 */
	private void getData_tijiao(final String fapiaotaitou) {
		loadingToast.show();

		new Thread(new Runnable() {
			@Override
			public void run() {
				// http地址
				String httpUrl = GetDataConfing.ip
						+ GetDataConfing.Action_shoppingAddOrderInformation;
				// params.put("type", type);
				// params.put("td", td);
				Map<String, Object> params = new HashMap<String, Object>();
				// params.put(GetDataQueue.Params_key,
				// GetDataConfing.Key_shoppingAddOrderInformation);
				params.put("userName", Utils.getUserAccount(mContext));
				params.put("addressId", addressId);
				params.put("invoiceTitle", fapiaotaitou);
				params.put("payTypeId", payId);
				params.put("shoppingCartList", selectProduct);
				params.put("couponItemId", couponid);
				params.put("SubtractMoney", jifen_youhui);
				params.put("SubtractPoint", jifen_num_totle - jifen_num_available);
				params.put("remark", et_note.getText().toString());
				List<NameValuePair> param = new ArrayList<NameValuePair>();
				param.add(new BasicNameValuePair(
						GetDataConfing.Key_shoppingAddOrderInformation,
						new Gson().toJson(params)));
				GetData.doPost(handler, httpUrl, param, 1);
			}
		}).start();

	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			loadingToast.dismiss();
			switch (msg.what) {
				case R.id.msgTimeOut:// 请求超时
					toast.showToast(getString(R.string.msg_timeout));
					break;
				case R.id.msgNetNotConnected:// 网络未连接
					toast.showToast(getString(R.string.msg_net_not_connected));
					break;
				case R.id.msgServerError:// 连接服务器失败
					toast.showToast(getString(R.string.msg_server_error));
					break;
				case R.id.msgResultSuccess:// 成功返回结果
					resultSuccess(msg);
					break;
				case R.id.msgResultFailure:// 失败
					toast.showToast(getString(R.string.msg_load_data_failure));
					break;
			}
		}

		private void resultSuccess(Message msg) {
			// TODO Auto-generated method stub
			String mString = msg.obj.toString();
			if (ShowGetDataError.isOkAndCodeIsNot1(
					mContext, mString)) {
				JsonMap<String, Object> info = JsonParseHelper
						.getJsonMap_JsonMap(mString, JsonKeys.Key_Info);
				if (info.size() == 0) {
					ShowGetDataError
							.showNetError(mContext);
				} else {
					JsonMap<String, Object> order = info;
					order.put("selectpayid", payId);
					String proListStr = "";
					for (JsonMap<String, Object> jsonMap : selectProduct2) {
						proListStr += jsonMap.getStringNoNull("ProductSaleName") + ",";
					}
					order.put("productName", proListStr);
					sendShopCartNumChangeBroadCast();
					goCommited(order);
				}
//				  List<JsonMap<String, Object>> map= JsonParseHelper.getJsonMap_List_JsonMap(mString, JsonKeys.Key_Info);
//				  if (map!=null) {
//					  goCommited(map.get(0).getStringNoNull("OrderNum"));
//				}
			}
		}

	};

	/**
	 * 通知其他页面 购物车数量改变
	 */
	private void sendShopCartNumChangeBroadCast() {
		sendBroadcast(new Intent(Constant.ACAT_SHOPCARTNUMCHANGE));
	}

	/**
	 * 获取填写信息的默认信息
	 */
	private void getData_getDefInfo() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("userAccount", Utils.getUserAccount(this));
		params.put("ShoppingCartList", selectProduct);
//		int addressIdInt = 0;
//		try {
//			addressIdInt = Integer.parseInt(addressId);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		params.put("addressId", addressIdInt);
//		int couponidInt = 0;
//		try {
//			couponidInt = Integer.parseInt(couponid);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		params.put("couponItemId", couponidInt);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_shoppingEditOrderInformation);
		queue.setCallBack(callBack);
		queue.setWhat(what_getdefinfo);
		queue.setParamsNoJson(params);

		getDataUtil.getData(queue);
	}

	private final int what_tijiao = 1;
	private final int what_getdefinfo = 2;
	private final int what_cancelcoupon = 3;
	private final int what_getJifen = 4;//获取优惠的金额
	private final int what_changeAddress = 5;//更换地址
	/**
	 * 与服务器通信的回调
	 */
	private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (what_tijiao == entity.what) {
					if (ShowGetDataError.isOkAndCodeIsNot1(
							mContext, entity.getInfo())) {
						List<JsonMap<String, Object>> info = JsonParseHelper
								.getJsonMap_List_JsonMap(entity.getInfo(),
										JsonKeys.Key_Info);
						if (info.size() == 0) {
							ShowGetDataError.showNetError(mContext);
						} else {
							JsonMap<String, Object> order = info.get(0);
//							goCommited(order.getStringNoNull("OrderNum"));
						}

					}
				} else if (what_getdefinfo == entity.what) {
					if (ShowGetDataError.isOkAndCodeIsNot1(
							mContext, entity.getInfo())) {
						flushDefInfo(JsonParseHelper.getJsonMap_JsonMap(
								entity.getInfo(), JsonKeys.Key_Info));

					}
				} else if (entity.what == what_getJifen) {  //"Info":{"Remainingmoney":1.00,"RemainingPoint":900.00}
					flushJifenAndPrice(JsonParseHelper.getJsonMap_JsonMap(
							entity.getInfo(), JsonKeys.Key_Info));
				} else if (what_cancelcoupon == entity.what) {
					if (ShowGetDataError.isOkAndCodeIsNot1(
							mContext, entity.getInfo())) {
						List<JsonMap<String, Object>> info = JsonParseHelper
								.getJsonMap_List_JsonMap(entity.getInfo(),
										JsonKeys.Key_Info);
						if (info.size() > 0) {
							flushJieSuanqingdan(info.get(0));
						}

					}
				} else if (what_changeAddress == entity.getWhat()) {
					if (ShowGetDataError.isOkAndCodeIsNot1(
							mContext, entity.getInfo())) {
						List<JsonMap<String, Object>> info = JsonParseHelper
								.getJsonMap_List_JsonMap(entity.getInfo(),
										JsonKeys.Key_Info);
						if (info.size() > 0) {
							flushBottomView(info.get(0));
						}

					}

				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};
	private float jifen_youhui = 0;

	private void flushJifenAndPrice(JsonMap<String, Object> jsonMap) {
		select_jifen_bt.setText(R.string.input_jifen);
		jifen_num_available = jsonMap.getFloat("RemainingPoint");
		jifen_youhui = jsonMap.getFloat("Remainingmoney");
		try {
			ActualPayPrice = ActualPayPrice - jifen_youhui;
			tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		jifen_availble_tv.setText("" + jsonMap.getFloat("RemainingPoint"));
		jifen_youhui_tv.setText(StringUtil.getFormatMoneyWithSignMust(jifen_youhui));
	}

	/**
	 * 去选择优惠券
	 */
	public void coupon_click(View v) {
		if (flog == 0) {
			//JsonMapOrListJsonMap2JsonUtil<String, Object> util = new JsonMapOrListJsonMap2JsonUtil<String, Object>();
			Intent intent = new Intent(this, ShoppingRivilegeActivity.class);
			intent.putExtra(ExtraKeys.Key_Msg2, selectProJson);
			intent.putExtra(ExtraKeys.Key_Msg3, TextUtils.isEmpty(addressId) ? "0" : addressId);
			startActivityForResult(intent, 3);
		} else if (flog == 1) {
			cancelCoupon();
//			loadingToast.show();
//			Map<String, Object> params = new HashMap<String, Object>();
//			params.put(GetDataQueue.Params_key,
//					GetDataConfing.Key_concelShoppingUsedCoupon);
//			params.put("userAccount", Utils.getUserAccount(this));
//			params.put("ShoppingCartList", selectProduct);
//			GetDataQueue queue = new GetDataQueue();
//			queue.setActionName(GetDataConfing.Action_concelShoppingUsedCoupon);
//			queue.setCallBack(callBack);
//			queue.setWhat(what_cancelcoupon);
//			queue.setParamsNoJson(params);
//
//			getDataUtil.getData(queue);
		}
	}

	private void cancelCoupon() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key,
				GetDataConfing.Key_shoppingUsedCoupon);
		params.put("userAccount", Utils.getUserAccount(mContext));
		params.put("ShoppingCartList", selectProduct);

		params.put("couponItemId", "0");
		addressId=TextUtils.isEmpty(addressId)?"0":addressId;
		params.put("addressId", addressId);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_shoppingUsedCoupon);
		queue.setCallBack(callBack);
		queue.setParamsNoJson(params);
		queue.setWhat(what_cancelcoupon);
		getDataUtil.getData(queue);
	}

	public void select_jifen_bt_click(View view) {
		if (((TextView) view).getText().toString().equals(getString(R.string.exchange))) {
			String jifen = jifen_ed.getText().toString().trim();
			if (TextUtils.isEmpty(jifen)) {
				toast.showToast(R.string.input_jifen);
			} else {
				int jifenInt = Integer.parseInt(jifen);

				if (jifenInt > jifen_num_available) {
					toast.showToast(R.string.input_jifen_max);
				} else {
					cal_jifen(jifenInt);
				}
			}
		} else {
			resetJifenAndPrice();
			select_jifen_bt.setText(R.string.exchange);
		}
	}

	private void resetJifenAndPrice() {
		jifen_num_available = jifen_num_totle;
		jifen_availble_tv.setText("" + jifen_num_available);

		ActualPayPrice = ActualPayPrice + jifen_youhui;
		tv_qian_yingfujine.setText(StringUtil.getFormatMoneyWithSignMust(ActualPayPrice));

		jifen_youhui = 0;
		jifen_youhui_tv.setText(StringUtil.getFormatMoneyWithSignMust("0"));

	}

	private void cal_jifen(int jifenInt) {
		//loadingToast.show();
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put(GetDataQueue.Params_key,
//				GetDataConfing.Key_Exchange_Point);
//		params.put("orderPrice",zongjia );
//		params.put("point", jifenInt);
//		params.put("userAccount",TnSharedPreferences.getUserName(this));
//
//		GetDataQueue queue = new GetDataQueue();
//		queue.setActionName(GetDataConfing.Action_UpdateOffset);
//		queue.setCallBack(callBack);
//		queue.setWhat(what_getJifen);
//		queue.setParamsNoJson(params);
//		getDataUtil.getData(queue);
	}
}
